package com.ex.phanso.gui;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import com.ex.phanso.Common;
import com.ex.phanso.Phanso;

public class Giaodien extends JFrame implements ActionListener{

	JPanel pn;
	JPanel pn1, pn2, pn3;
	JPanel pnKetqua;
	
	JLabel lb1, lb2, lbketqua;
	JTextField txttu1, txtmau1, txttu2, txtmau2;
	
	JButton btnCong, btnTru, btnNhan, btnChia;
	
	Phanso a, b;
	
	public Giaodien() {
		
		pn = new JPanel();
		pn.setLayout(new BoxLayout(pn, BoxLayout.Y_AXIS));
		
		pn1 = new JPanel();
		pn2 = new JPanel();
		pn3 = new JPanel();
		pnKetqua = new JPanel();
		
		pn1.setLayout(new FlowLayout(FlowLayout.LEFT));
		pn2.setLayout(new FlowLayout(FlowLayout.LEFT));
//		pn3.setLayout(new FlowLayout(FlowLayout.LEFT));
//		pnKetqua.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		lb1 = new JLabel("Phan so1");
		lb2 = new JLabel("Phan so2");
		lbketqua = new JLabel("3/5");
		
		txttu1 = new JTextField("", 10);
		txtmau1 = new JTextField("", 10);
		txttu2 = new JTextField("", 10);
		txtmau2 = new JTextField("", 10);
		
		btnCong = new JButton("Cong");
		btnTru = new JButton("Tru");
		btnNhan = new JButton("Nhan");
		btnChia = new JButton("Chia");
		
		btnCong.addActionListener(this);
		btnTru.addActionListener(this);
		btnNhan.addActionListener(this);
		btnChia.addActionListener(this);
		
		txttu1.setSize(50, 40);
		txtmau1.setSize(50, 40);
		txttu2.setSize(50, 40);
		txtmau2.setSize(50, 40);
			
		
			
		//add panel1
		pn1.add(lb1);
		pn1.add(txttu1);
		pn1.add(txtmau1);
		//add panel2
		pn2.add(lb2);
		pn2.add(txttu2);
		pn2.add(txtmau2);
		//
		Border title = new TitledBorder("Ket qua");
		pnKetqua.setBorder(title);
		pnKetqua.add(lbketqua);
		
		//
		pn3.add(btnCong);
		pn3.add(btnTru);
		pn3.add(btnNhan);
		pn3.add(btnChia);
		
		//
		pn.add(pn1);
		pn.add(pn2);
		pn.add(pnKetqua);
		pn.add(pn3);
		
		this.add(pn);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if (e.getSource() == btnCong) {
			System.out.println("cong");
			
			String m1 = txtmau1.getText();
			String t1 = txttu1.getText();
			
			String m2 = txtmau2.getText();
			String t2 = txttu2.getText();
			
			if (t1.length()>0 && m1.length()>0) {
				try {
					int a = Integer.parseInt(t1);
					int b = Integer.parseInt(m1);
					int c = Integer.parseInt(t2);
					int d = Integer.parseInt(m2);
					
					Phanso ps1 = new Phanso(a, b);
					Phanso ps2 = new Phanso(c, d);
					
					Common cm = new Common();
					Phanso kq = (Phanso) cm.cong(ps1, ps2);
									
					lbketqua.setText( kq.toString() );
				} catch (Exception e2) {
					lbketqua.setText("Du lieu loi khong the chuyen doi ve integer duoc.");
				}				
			}
			
		}
		else if(e.getSource() == btnTru) {
			System.out.println("tru");	
		}
	}
	
	public void setPhanso(Phanso a, Phanso b){
		this.a = a;
		this.b = b;
		
		txttu1.setText( a.getTu() + "" );
		txtmau1.setText( a.getMau() + "" );
		//
		txttu2.setText( b.getTu() + "" );
		txtmau2.setText( b.getMau() + "" );
		
	}
	
}